<?php
/**
 * Remove a document from a ressource
 * @package Ressource
 */
use Fox\Behaviors;
use Hive\Ressource;
use Fox\Generator;
$xview = Xview::instance();

$rid = $xview->val("rid");
$wid = $xview->val("wid");
if(empty($wid))
{
	Xview::instance()->systemError();exit;
}
/* @var $contact Hive\Ressource */
$contact = Xview::instance()->component("ressource", "object");
$wish = $contact->findWishes("$wid:$rid");
$label = $wish->joblist;
if(Xview::instance()->val("confirmation"))
{
	try {
		$contact->deleteFromWishes($wish);
		$cdate = null;
		foreach($contact->wishes() as $iWish) {
			if($iWish->cdate > $cdate && $iWish->did != $wid) {
				$cdate = $iWish->cdate;
				$currentWish = $iWish;
			}
		}
		if(!empty($currentWish)) {
			$currentWish->current = 1;
			$contact->replaceWishes($currentWish);
		}
		$contact->prepareUpdate(Ressource::CHANGE_WISHES_DELETE);
		$contact->update();
		Fox\Message::Information(Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_DELETE,$label));
		echo $xview->messages();
		exit;
	}
	catch (Fox\Object\Exception\Save $e)
	{
		Fox\Message::Error($e->getMessage());
	}
}

$reloadGen = Generator::ajaxReload("contentCol",Generator::url("ressource","wishes",null,array("rid" => $rid)));
$yesAction = Generator::ajax(Generator::url('ressource','wishes','delete',array('rid' => $rid,'wid' => $wid )),
							'&confirmation=true',
							$reloadGen,'hdeleteForm',null,2
);

$popup = new Fox\Popup\ToConfirm("deleteForm",
					Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_DELETE_HDR),
					Msg(Fox\Msg::BUNDLE,Fox\Msg::MSG_DELETE_MSG_CONF,$label),
					$yesAction,null,400);
echo $popup->Display();